﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;

namespace mamnontuoitho7.backend
{
    public partial class ClassEdit : System.Web.UI.Page
    {
        private Class _class = new Class();
        private ClassCategory _classCategory = new ClassCategory();


        //Contain involve notice 
        private string _notice;
        private String _classID = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionInfo).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {
                    if (!IsPostBack)
                    {
                        if (Request.QueryString["hidClassID"] != null)
                        {
                            _classID = Request.QueryString["hidClassID"].ToString();
                            loadClassCategory();
                            loadClassInformation(_classID);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }


        /// <summary>
        /// Put Class Category into Drop down list
        /// </summary>
        public void loadClassCategory()
        {
            DataTable dttClassCategory = _classCategory.GetAllClassCategory();
            ddlClassCategory.DataSource = dttClassCategory;
            ddlClassCategory.DataTextField = "ClassCategoryName";
            ddlClassCategory.DataValueField = "ClassCategoryID";
            ddlClassCategory.DataBind();
        }

        /// <summary>
        /// Load Class information from database
        /// </summary>
        public void loadClassInformation(String classID)
        {
            try{
                DataRow row = _class.GetClassInformation(classID);
            ddlClassCategory.SelectedValue = row["ClassCategoryID"].ToString();
            txtClassName.Text = row["ClassName"].ToString();
            txtClassPosition.Text = row["ClassPosition"].ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }

        /// <summary>
        /// Create Class Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btClassEdit_Click(object sender, EventArgs e)
        {
            try
            {

                if (txtClassName.Text.Trim() == "")
                {
                    lblNoticeClass.Text = "Tên lớp không được bỏ trống.";
                    lblNoticeClass.Visible = true;
                    return;
                }

                if (Request.QueryString["hidClassID"] != null)
                {
                    _notice = "";
                    bool check = _class.UploadClass(Request.QueryString["hidClassID"].ToString(), ddlClassCategory.SelectedValue.ToString(), txtClassName.Text.Trim(), txtClassPosition.Text.Trim());

                    //Check inserted or not
                    if (check)
                    {
                        _notice = "Đã cập nhật lớp thành công.";
                        //loadClassInformation();
                    }
                    else
                    {
                        _notice = "Có lỗi hệ thống trong khi tạo lớp.";
                    }

                    lblNoticeClass.Visible = true;
                    lblNoticeClass.Text = _notice;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }


        /// <summary>
        /// Reset Create Class Page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btClassReset_Click(object sender, EventArgs e)
        {
            try
            {
                loadClassInformation(Request.QueryString["hidClassID"].ToString());
                lblNoticeClass.Visible = false;
                lblNoticeClass.Text = "";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }


    }
}